<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\TestingResultAttribute;

use ResultAttributeService;

class TestingResultAttributeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_result_attribute.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.testing_result_attribute.title.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_result_attribute.title.add'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(TestingResultAttribute::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('backend.testing_result_attribute.column.id'))->sortable();
            $grid->column('title',trans('backend.testing_result_attribute.column.title'));
            $grid->tag(trans('backend.testing_result_attribute.column.tag'));
            $grid->note(trans('backend.testing_result_attribute.column.note'))->value(function($note){
                return str_limit($note, 50, '...');
            });
            $grid->disableExport();

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TestingResultAttribute::class, function (Form $form) {
            $form->text('title', trans('backend.testing_result_attribute.column.title'))->rules('required');
            $form->text('tag', trans('backend.testing_result_attribute.column.tag'))->rules('required');
            $form->editor('note', trans('backend.testing_result_attribute.column.note'))->attribute('rows', 20)->rules('required');

        });
    }

    public function options(){
        return ResultAttributeService::getAttrOptions();
    }
}
